﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;
using System.Windows.Forms;


namespace MockPrjTeam1.Pages.Forms
{
    public partial class FacilityDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

             
            var id = Convert.ToString(Request.QueryString["id"]);
            if (!string.IsNullOrEmpty(id))
            {
               
                    var biz = new FacilityBiz();
                    var result = new Facility();
                    result = biz.GetOne(Convert.ToInt32(id));
                    DrDFacilityType.Items.Insert(0, result.FacilityType);
                    txtFacilityDescription.Text = result.Description;
                    hdfContact.Value = result.LeadContactID.ToString();
                    if (result.LeadContactID != 0)
                    {
                        ContactBiz contactBiz = new ContactBiz();
                        Contact contact = contactBiz.GetOne(result.LeadContactID);
                        if (contact != null)
                        {
                            txtLeadContact.Text = contact.ContactName;
                        }
                    }

                    hdfRoomHost.Value = result.RoomHostID.ToString();
                    if (result.RoomHostID != 0)
                    {
                        ContactBiz contactBiz = new ContactBiz();
                        Contact contact = contactBiz.GetOne(result.RoomHostID);
                        if (contact != null)
                        {
                            txtRoomHost.Text = contact.ContactName;
                        }
                    }

                    txtCapacity.Text = result.RoomCapacity;
                    txtRoomSize.Text = result.RoomSize;
                    ckbRoomConnectivity.Checked = result.RoomConnectivity;
                    DrDConnectivityType.Items.Insert(0, result.ConnectivityType);
                    txtWireless.Text = result.WIAccessInfor;
                    ckbEquipmentAvailable.Checked = result.EquipmentAvailable;
                    txtRoomReqipmentNotes.Text = result.RQNote;
                    ckbActive.Checked = result.Active;
                }
              


            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/FacilitiesList.aspx", false);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var id = Convert.ToString(Request.QueryString["id"]);
            var facility = new Facility();
            if (!string.IsNullOrEmpty(id))
            {
                facility.FacilityID = Convert.ToInt32(id);
            }
            facility.FacilityType = DrDFacilityType.SelectedValue;
            facility.Description = txtFacilityDescription.Text;
            facility.LeadContactID = !string.IsNullOrEmpty(hdfContact.Value) ? Convert.ToInt32(hdfContact.Value) : 0;
            facility.RoomHostID = !string.IsNullOrEmpty(hdfRoomHost.Value) ? Convert.ToInt32(hdfRoomHost.Value) : 0;
            facility.RoomCapacity = txtCapacity.Text;
            facility.RoomSize = txtRoomSize.Text;
            if (ckbRoomConnectivity.Checked) facility.RoomConnectivity = true;
            else facility.RoomConnectivity = false;
            facility.ConnectivityType = DrDConnectivityType.SelectedValue;
            facility.WIAccessInfor = txtWireless.Text;
            if (ckbEquipmentAvailable.Checked) facility.EquipmentAvailable = true;
            else facility.EquipmentAvailable = false;
            facility.RQNote = txtRoomReqipmentNotes.Text;
            if (ckbActive.Checked) facility.Active = true;
            else facility.Active = false;

            var biz = new FacilityBiz();
            bool result;
            if (!string.IsNullOrEmpty(id))
                result = biz.EditFacility(facility);
            else result = biz.Save(facility);
            if (result)
            {
                Response.Redirect("../Views/FacilitiesList.aspx", false);
            }

        }





    }


}
